home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 27 / CU Amiga Magazine's Super CD-ROM 27 (1998)(EMAP Images)(GB)[!][issue 1998-10].iso / CUCD / PowerPC / vbcc / machines / amigawos / libsrc / stdio / fopen.c < prev    next >
C/C++ Source or Header  |  1998-08-02  |  931b  |  54 lines

  1. #include <stddef.h>
  2. #include <stdio.h>
  3. #include <limits.h>
  4. #include <stdarg.h>
  5. #include <stdlib.h>
  6. #include <errno.h>
  7.  
  8. #define OLDFILE 0
  9. #define NEWFILE 1
  10. #define RWFILE  2
  11.  
  12. extern FILE *_firstfile,*_lastfile;
  13.  
  14. FILE *fopen(const char *name,const char *mode)
  15. {
  16.   FILE *f;
  17.   long openmode=0;
  18.   int append;
  19.  
  20.   openmode = *mode=='w' ? NEWFILE : OLDFILE;
  21.   append = *mode=='a';
  22.   if (!(f = malloc(sizeof(FILE)))) {
  23.     errno = ENOMEM;
  24.     return (0);
  25.   }
  26.   f->count = 0;
  27.   f->base = 0;
  28.   f->bufsize = 0;
  29.   f->next = 0;
  30.   f->flags = *mode++=='r' ? _READABLE : _WRITEABLE;
  31.   if (*mode=='b')
  32.     mode++;
  33.   if (*mode=='+') {
  34.     f->flags |= _READABLE|_WRITEABLE;
  35.     openmode = RWFILE;
  36.   }
  37.  
  38.   if (!_open((char *)f,name,openmode)) {
  39.     free(f);
  40.     return (0);
  41.   }
  42.   if(_lastfile) {
  43.     _lastfile->next=f;
  44.     f->prev=_lastfile;
  45.     _lastfile=f;
  46.   }
  47.   else
  48.     _firstfile=_lastfile=f;
  49.  
  50.   if (append)
  51.     fseek(f,0,SEEK_END);
  52.   return(f);
  53. }
  54.